package eSchool.GUI.Controls;

import eSchool.BL.CriteriosPesquisa;
import eSchool.GUI.Components.IUIConector;
import eSchool.GUI.Components.UIConectorDisciplina;
import eSchool.Utils.TextHelper;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

public class panDisciplinas extends JPanelExtended {

    public panDisciplinas() {
        initComponents();
        panDataGrid1.setEventListner(this);
        panDataGrid2.setEventListner(this);
        panDetalheDisciplina1.setEventListner(this);              
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabDisciplina = new javax.swing.JTabbedPane();
        panPesquisa = new javax.swing.JPanel();
        panDataGrid1 = new eSchool.GUI.Controls.panDataGrid();
        panDetalhe = new javax.swing.JPanel();
        panDetalheDisciplina1 = new eSchool.GUI.Controls.panDetalheDisciplina();
        panAdicionaAlunos = new javax.swing.JPanel();
        panDataGrid2 = new eSchool.GUI.Controls.panDataGrid();
        jButton1 = new javax.swing.JButton();

        setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout panPesquisaLayout = new javax.swing.GroupLayout(panPesquisa);
        panPesquisa.setLayout(panPesquisaLayout);
        panPesquisaLayout.setHorizontalGroup(
            panPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDataGrid1, javax.swing.GroupLayout.DEFAULT_SIZE, 681, Short.MAX_VALUE)
                .addContainerGap())
        );
        panPesquisaLayout.setVerticalGroup(
            panPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDataGrid1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabDisciplina.addTab("Pesquisa", panPesquisa);

        javax.swing.GroupLayout panDetalheLayout = new javax.swing.GroupLayout(panDetalhe);
        panDetalhe.setLayout(panDetalheLayout);
        panDetalheLayout.setHorizontalGroup(
            panDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panDetalheLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDetalheDisciplina1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panDetalheLayout.setVerticalGroup(
            panDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panDetalheLayout.createSequentialGroup()
                .addComponent(panDetalheDisciplina1, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabDisciplina.addTab("Ficha Disciplina", panDetalhe);

        jButton1.setText("Adicionar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panAdicionaAlunosLayout = new javax.swing.GroupLayout(panAdicionaAlunos);
        panAdicionaAlunos.setLayout(panAdicionaAlunosLayout);
        panAdicionaAlunosLayout.setHorizontalGroup(
            panAdicionaAlunosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panAdicionaAlunosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panAdicionaAlunosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panDataGrid2, javax.swing.GroupLayout.DEFAULT_SIZE, 681, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        panAdicionaAlunosLayout.setVerticalGroup(
            panAdicionaAlunosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panAdicionaAlunosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDataGrid2, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        tabDisciplina.addTab("Adiciona Alunos", panAdicionaAlunos);

        add(tabDisciplina, "cardDiciplinas");
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {

            uiConector.addInscricao(panDataGrid2.getSelectRowValue(0));
        } catch (Exception ex) {
            Logger.getLogger(panDisciplinas.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jButton1ActionPerformed

   
    @Override
    public void GridDoubleClickEvent(Object sender, Object obj) {
        try {
            
            DataGrid dg = (DataGrid) sender;
                       
            int row = ((Integer) obj).intValue();
            
            String num = TextHelper.EmptyString;
            if(dg.getValueAt(row, 0) != null)
                num = dg.getValueAt(row, 0).toString();

            CriteriosPesquisa cr = new CriteriosPesquisa();
            cr.add("codDisciplina",num);
            
            TableModel tableModel = uiConector.pesquisaDetalhe(cr);
            TableModel tableModelAlunosInscritos = uiConector.getDadosExtra(0, cr);
            TableModel tableModelDocentes = uiConector.getDadosExtra(1, new CriteriosPesquisa());
            this.panDetalheDisciplina1.setDetailDataSource(tableModel);
            this.panDetalheDisciplina1.setDetailBindIdDisciplinaIndex(0);
            this.panDetalheDisciplina1.setDetailBindDisciplinaIndex(1);
            this.panDetalheDisciplina1.setListDataSource(tableModelAlunosInscritos);
            this.panDetalheDisciplina1.setDocentes(tableModelDocentes);
            this.panDetalheDisciplina1.DataBind();
            
            this.setSelectedTab(1);
        
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível identificar o objecto escolhido. \n" + ex.getMessage()); 
            Logger.getLogger(panTabFicha.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public void SearchButtonClickEvent(Object Sender, Object obj) {
        try {
            
            CriteriosPesquisa cr = (CriteriosPesquisa)obj;
            
            this.panDataGrid1.setDataSource(uiConector.pesquisaLista(cr));
            this.panDataGrid1.DataBind();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto escolhido. \n" + ex.getMessage()); 
            Logger.getLogger(panDataGrid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
     
     public void setObjectoTrabalho(UIConectorDisciplina uiConector){
        try {
            this.uiConector = uiConector;
            
            TableModel tableModel = uiConector.getDetalhe();
            this.panDetalheDisciplina1.setDetailDataSource(tableModel);
            this.panDetalheDisciplina1.setDetailBindIdDisciplinaIndex(0);
            this.panDetalheDisciplina1.setDetailBindDisciplinaIndex(1);
            
            CriteriosPesquisa cr = new CriteriosPesquisa();
            cr.add("codDisciplina", "0");
            TableModel tableModelAlunosInscritos = uiConector.getDadosExtra(0, cr);
            TableModel tableModelDocentes = uiConector.getDadosExtra(1, new CriteriosPesquisa());
            TableModel tableModelAlunos = uiConector.getDadosExtra(2, new CriteriosPesquisa());
            this.panDetalheDisciplina1.setDocentes(tableModelDocentes);
            this.panDetalheDisciplina1.setListDataSource(tableModelAlunosInscritos);
            this.panDataGrid2.setDataSource(tableModelAlunos);
            this.panDetalheDisciplina1.DataBind();
            this.panDataGrid2.DataBind();
            
            DataGridColumnIdentifiers dcs = new DataGridColumnIdentifiers();
            
            dcs.add("Código", "codDisciplina", 50);
            dcs.add("Disciplina", "nomeDisciplina", 250);
            this.panDataGrid1.setDataGridColumnIdentifiers(dcs);
            this.panDataGrid1.setDataSource(this.uiConector.getLista());
            this.panDataGrid1.DataBind();
     
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto. \n" + ex.getMessage()); 
            Logger.getLogger(panDisciplinas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     
       
    public void setSelectedTab(int index) {
        this.tabDisciplina.setSelectedIndex(index);
    }
    
    @Override
    public void SaveButtonClickEvent(Object Sender, Object obj) {
       uiConector.gravaDetalhe(panDetalheDisciplina1.getDataSource());
    }
    
    
    private UIConectorDisciplina uiConector;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel panAdicionaAlunos;
    private eSchool.GUI.Controls.panDataGrid panDataGrid1;
    private eSchool.GUI.Controls.panDataGrid panDataGrid2;
    private javax.swing.JPanel panDetalhe;
    private eSchool.GUI.Controls.panDetalheDisciplina panDetalheDisciplina1;
    private javax.swing.JPanel panPesquisa;
    private javax.swing.JTabbedPane tabDisciplina;
    // End of variables declaration//GEN-END:variables

}
